package com.guomao.demoe;

import java.util.concurrent.locks.ReentrantLock;

public class TicketSeller implements Runnable {
    private int tickets = 100; // 共享资源：剩余票数

//    private final Object lock = new Object();

    private ReentrantLock lock = new ReentrantLock();
/*    @Override
    public void run() {
        while (true) {
            if (tickets > 0) {
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                tickets--;
                System.out.println(Thread.currentThread().getName() + "卖出一张票,剩余" + tickets);
            }
        }
    }*/


    //同步方法
/*    @Override
    public synchronized void run() {
        while (true) {
            if (tickets > 0) {
                try {
                    Thread.sleep(50);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                tickets--;
                System.out.println(Thread.currentThread().getName() + " 卖出一张票，剩余票数: " + tickets);
            } else {
                System.out.println(Thread.currentThread().getName() + " 发现票已售罄");
                break;
            }
        }
    }*/

/*    @Override
    public void run() {
        while (true) {
//            同步代码块
            synchronized (lock) {
                if (tickets > 0) {
                    try {
                        Thread.sleep(50);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    tickets--;
                    System.out.println(Thread.currentThread().getName() + " 卖出一张票，剩余票数: " + tickets);
                } else {
                    System.out.println(Thread.currentThread().getName() + " 发现票已售罄");
                    break;
                }
            }
        }
    }*/

/*    @Override
    public void run() {
        while (true) {
            lock.lock();
            if (tickets > 0) {
                try {
                    Thread.sleep(50);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                tickets--;
                System.out.println(Thread.currentThread().getName() + " 卖出一张票，剩余票数: " + tickets);
            } else {
                System.out.println(Thread.currentThread().getName() + " 发现票已售罄");
                break;
            }
            lock.unlock();
        }
    }*/

    @Override
    public void run() {
        while (true) {
            lock.lock();
            if (tickets > 0) {
                try {
                    Thread.sleep(50);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                tickets--;
                System.out.println(Thread.currentThread().getName() + " 卖出一张票，剩余票数: " + tickets);
            } else {
                System.out.println(Thread.currentThread().getName() + " 发现票已售罄");
                break;
            }
            lock.unlock();
        }
    }
}